package com.hong.qrpc.handler.in.listener;

import com.hong.qrpc.client.NettyTransportClient;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import lombok.extern.slf4j.Slf4j;

/**
 * @Author HQT
 * @Date 2022/10/19 11:28
 */
@Slf4j
public class CloseListener extends ChannelInboundHandlerAdapter {
    private NettyTransportClient client;

    public CloseListener() {
    }

    public CloseListener(NettyTransportClient nettyClient) {
        client = nettyClient;
    }

    /**
     * 可以监听服务端意外或者主动断开
     */
    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        log.debug("断开链接" + ctx.channel().localAddress().toString());
        client.setClosed(true);
        client.close();
    }
}
